<template>
    <div class="copyEntry">
        <!-- 来源名称 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" top="5vh" custom-class="import-dialog" :close-on-click-modal="false">
            <div>
                <el-form :inline="true" :model="indexClause" label-width="105px">
                    <el-form-item label="来源名称:">
                        <el-input v-model="indexSourceCategoryDetailPo.indexName" size="mini"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addFileSourceCategoryNode">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { Component, Vue, Watch, Prop } from 'vue-property-decorator';
@Component({
    name: 'CategoryNodeDialog',
    components: {
    },
})
export default class CategoryNodeDialog extends Vue {
    @Prop() categoryNodeDialog;
    @Prop() categoryNodeType;
    @Prop() indexItem;
    private dialogVisible: boolean = false;
    nodeName: string = '';
    title: string = '';
    indexSourceCategoryDetailPo = {
        indexDirectoryCode: '',
        indexType: '1', // 指标类型：1:监管类 2:合同类
        parentCode: '', // 来源类型
        indexName: '', // 指标来源名称
    }

    @Watch('categoryNodeDialog')
    watchCategoryNodeDialog(val) {
        this.dialogVisible = val;
        if (val == true) {
            this.title = '来源名称' + this.categoryNodeType;
            if (this.categoryNodeType == '新增') {
                this.indexSourceCategoryDetailPo.parentCode = this.indexItem.id;
            } else if (this.categoryNodeType == '修改') {
                this.indexSourceCategoryDetailPo.indexDirectoryCode = this.indexItem.id;
                this.indexSourceCategoryDetailPo.indexName = this.indexItem.label;
            }
            this.indexSourceCategoryDetailPo.indexType = this.indexItem.indexType;
        }
    }

    @Watch('dialogVisible')
    watchDialogVisible(val) {
        if (val == false) {
            this.indexSourceCategoryDetailPo.parentCode = '';
            this.indexSourceCategoryDetailPo.indexName = '';
            this.indexSourceCategoryDetailPo.indexDirectoryCode = '';
            this.$emit('emitCategoryNodeDialog', false, '')
        }
    }
    // 添加文件来源分类节点
    addFileSourceCategoryNode() {
        if (this.indexSourceCategoryDetailPo.indexName == '') {
            this.yh_message_warn('来源名称不可为空')
            return
        }
        if (this.categoryNodeType == '新增') {
            if (['0', '1', '2'].includes(this.indexSourceCategoryDetailPo.parentCode)) {
                this.$axios.post('/zhiguan/indexArticle/directory/insert', { ...this.indexSourceCategoryDetailPo }).then((res: any) => {
                    this.yh_message_success('新增成功!');
                    this.dialogVisible = false;
                    this.$emit('emitCategoryNodeDialog', false, '刷新接口')
                });    
            } else {
                this.$axios.post('/zhiguan/indexArticle/article/insert', { 
                    articleName: this.indexSourceCategoryDetailPo.indexName,
                    indexDirectoryCode: this.indexSourceCategoryDetailPo.parentCode,
                    indexType: this.indexSourceCategoryDetailPo.indexType
                 }).then((res: any) => {
                    this.yh_message_success('新增成功!');
                    this.dialogVisible = false;
                    this.$emit('emitCategoryNodeDialog', false, '刷新接口')
                }); 
            }
        } else if (this.categoryNodeType == '修改') {
            this.$axios.post('/zhiguan/indexArticle/directory/update', { ...this.indexSourceCategoryDetailPo }).then((res: any) => {
                this.yh_message_success('修改成功!');
                this.dialogVisible = false;
                this.$emit('emitCategoryNodeDialog', false, '刷新接口')
            });
        }
    }
}
</script>
<style lang="scss" scoped>
</style>
